package com.meixiang.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.services.ServiceDetail;
import com.meixiang.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目说明view  用于一排两个textview 。不定长度
 * author:xiejingwen
 */
public class ProjectView extends LinearLayout{
    private Context mContext;
    List<ServiceDetail.InstructionListBean> data;
    public ProjectView(Context context) {
        this(context, null);
    }

    public ProjectView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public ProjectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
        setOrientation(VERTICAL);
        data=new ArrayList<>();

    }

    /**
     * 添加数据
     * @param data
     */
    public void setData(List<ServiceDetail.InstructionListBean> data){
        this.data.clear();
        this.data.addAll(data);
        initView();
    }

    private void initView(){
        removeAllViews();

        for (int i=0;i<data.size();i++){
            LinearLayout layout=new LinearLayout(mContext);
            LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
            if (i>0){
                params.topMargin=DensityUtil.dip2px(mContext,15);
            }
            layout.setLayoutParams(params);
            layout.setOrientation(HORIZONTAL);
            layout.setGravity(Gravity.CENTER_VERTICAL);
            TextView leftText=new TextView(mContext);
            leftText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            leftText.setText(data.get(i).getInstructionName() + ":");
            leftText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            leftText.setTextColor(ContextCompat.getColor(mContext, R.color.black_8));
            layout.addView(leftText);
            TextView rightText=new TextView(mContext);
            rightText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            rightText.setText(data.get(i).getIndexValue());
            rightText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            rightText.setTextColor(ContextCompat.getColor(mContext, R.color.black_3));
            rightText.setPadding(DensityUtil.dip2px(mContext, 4), 0, 0, 0);
            layout.addView(rightText);
            addView(layout);
        }
    }
}
